package nuc.edu.employmentservices.controller;

import com.mysql.cj.Session;
import com.mysql.cj.protocol.x.ReusableInputStream;
import nuc.edu.employmentservices.dao.GraduatesMapper;
import nuc.edu.employmentservices.enity.*;
import nuc.edu.employmentservices.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.SQLOutput;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class GraduatesController {
    @Autowired
    private GraduatesService graduatesService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClassesService classesService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DirectionService directionService;


    @GetMapping("/memberlist")
    public String memberlist(Model model){
        List<Graduates> graduatesList = graduatesService.getAllGraduates();
        Integer num=graduatesService.Getgranum();
        model.addAttribute("graduatesList1",graduatesList);
        model.addAttribute("num",num);
        return "memberlist";
    }

    @RequestMapping("/toaddgradutes")
    public String toaddgradutes(Model model){
        List<Classes> classesList = classesService.getAllClasses();
        model.addAttribute("classesList",classesList);

        List<Company> companyList = companyService.getAllCompany();
        model.addAttribute("companyList",companyList);

        List<Direction> directionList = directionService.getAllDirection();
        model.addAttribute("directionList",directionList);

        return "toaddgradutes";
    }

    @PostMapping("/addgradutes")
    public String addgradutes(Graduates graduates) throws ParseException {
        graduatesService.insertGraduates(graduates);
        User user = new User();
        user.setUserName(graduates.getUser().getUserName());
        user.setUseraccount(graduates.getUser().getUseraccount());
        user.setUserPassword(graduates.getUser().getUserPassword());
        user.setGraID(graduates.getGraID());
        user.setTypes(graduates.getUser().getTypes());
        userService.insertUser(user);
        return "/success";
    }

    @RequestMapping("/toupdategradutes/{updateid}")
    public String toupdategradutes(@PathVariable("updateid") Integer updateid, HttpSession session){
        Graduates graduates = graduatesService.getGraduatesById(updateid);
        session.setAttribute("upgraduates",graduates);

        return  "redirect:/toupda";
    }

    @RequestMapping("/toupda")
    public String toupda(Model model,HttpSession session){
        Graduates graduates =(Graduates)session.getAttribute("upgraduates");
        model.addAttribute("upgraduate",graduates);

        List<Classes> classesList = classesService.getAllClasses();
        model.addAttribute("classesList",classesList);

        List<Company> companyList = companyService.getAllCompany();
        model.addAttribute("companyList",companyList);

        List<Direction> directionList = directionService.getAllDirection();
        model.addAttribute("directionList",directionList);
        return "toupdategradutes";
    }

    @RequestMapping("/doupgradutes")
    public String doupgradutes(Graduates graduates){
        graduatesService.updateGraduatesById(graduates);
        User user = new User();
        user.setTypes(graduates.getUser().getTypes());
        user.setUserPassword(graduates.getUser().getUserPassword());
        user.setUseraccount(graduates.getUser().getUseraccount());
        user.setUserName(graduates.getUser().getUserName());
        user.setGraID(graduates.getGraID());

        userService.updateUserBy(user);
        return "/success";
    }

    @RequestMapping("/deletegradutes/{deleteid}")
    public String deletegradutes(@PathVariable("deleteid") Integer deleteid){
        graduatesService.deleteGraduates(deleteid);
        userService.deleteUserById(deleteid);
        return "redirect:/dodeletegradutes";
    }

    @RequestMapping("/dodeletegradutes")
    public String dodeletegradutes(){
        return "/success";
    }


}
